﻿package  {
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	
	public class hero extends MovieClip {
		
		private const gravity:int = 2;
		private const max_speed:int = 8;
		
		public const walkspeed:int = 4;
		public const jumpspeed:int = 30;
		public const doublejumpspeed = 25;
		
		public const start_x:int = 50;
		public const start_y:int = 50;
		
		public const inv_time:int=30;
		
		public var y_speed:int;
		public var x_speed:int;
		
		public var left:Boolean;
		public var up:Boolean;
		public var right:Boolean;
		public var space:Boolean;
		public var can_jump:Boolean;
		public var invulnerable:int;
		
		
		public var facingRight:Boolean; // Para saber que nuestro personaje esta viendo a la derecha
		
		var s_jump:sound=new sound("sound/hero_jump.mp3",0,0,1);
		var s_damage:sound=new sound("sound/hero_damage.mp3",0,0,1);
		var s_step:sound=new sound("sound/hero_step.mp3",0,0,-1);


		//New mechanics
		// doble salto
		public var number_jumps:int; // numero de saltos para llevar control del doble salto
		public var double_jump:Boolean; // bandera para que el update del heroe haga un salto adicional
		public var enabled_double_jump:Boolean; // Si esta permitida la mecanica del doble salto
			
		// dash
		public var enable_Dashing:Boolean;
		public var dashTime;
		
		// eras para indicarle al juego que es momento de brincar de nivel
		// o de reiniciar el propio nivel porque el personaje pelo pelotas. Se chequean en el update del 
		// main
		public var flag_died:Boolean;
		
		
		public var hp:int;
		
		public function hero() {
			
			//addEventListener(KeyboardEvent.KEY_DOWN, key_down);
			//this.addEventListener(KeyboardEvent.KEY_UP, key_up);
			
			// Desbloquear mecanica para testing
			enabled_double_jump= true;
			enable_Dashing= false;
			invulnerable=inv_time;
			facingRight= true;
			
			flag_died= false;
			
			
		} 
		
		public function updateHero() {
			
			if(invulnerable<inv_time)invulnerable--;
			if(invulnerable<0)invulnerable=inv_time;
			
			this.alpha= 1.0;
			if(invulnerable!=inv_time) this.alpha= Math.random();
			
			if (hp < 0) flag_died= true;
			
			y_speed += gravity;
			if (y_speed < -15) y_speed= -15; // Velocidad de ascenso maxima
			
			if(left){
				x_speed -= walkspeed;
			}
			if(right){
				x_speed += walkspeed;
			}
			
			if(up && can_jump){
				y_speed -= jumpspeed;
				s_jump.play();
				can_jump = false;
			}
			if (double_jump) {
				y_speed -= doublejumpspeed;
				s_jump.play();
				double_jump= false;
			}
			
			if(y_speed > max_speed){
				y_speed = max_speed;
			}
			
			
			this.y += y_speed;
			this.x += x_speed;
				
			x_speed = 0;
		}
		
		public function hero_key_down(event:KeyboardEvent){
			
			if(event.keyCode == 37){
				
				if(!left && can_jump)s_step.play();
				left = true;
				this.gotoAndStop(5);
				facingRight= false;
			}
			if(event.keyCode == 38){
				up = true;
				//s_step.stop();
				if (enabled_double_jump && number_jumps==1) double_jump=true;
				number_jumps=number_jumps+1;
			}
			if(event.keyCode == 39){
				//s_step.stop();
				if(!right && can_jump)s_step.play();
				right = true;
				this.gotoAndStop(2);
				facingRight= true;
			}
			if(event.keyCode == 40){
				if ( facingRight) this.gotoAndStop(3);
				if (!facingRight) this.gotoAndStop(6);
			}
			if (event.keyCode == 32 && enable_Dashing) {
				trace("dash");
			}
		}
		
		public function hero_key_up(event:KeyboardEvent){
			if(event.keyCode == 37){
				left = false;
				this.gotoAndStop(4);
				if (s_step != null) s_step.stop();
			}
			if(event.keyCode == 38){
				up = false;
			}
			if(event.keyCode == 39){
				right = false;
				this.gotoAndStop(1);
				if (s_step  != null) s_step.stop();	
			}
			if(event.keyCode == 40){
				if ( facingRight) this.gotoAndStop(1);
				if (!facingRight) this.gotoAndStop(4);
			}
		}
		
		// El heroe recibe un impacto y retotrocede en direccion opuesta !
		// pierde HP
		public function impactGround() {
			
			if(invulnerable==inv_time){
				
				s_damage.play();
				//y_speed= y_speed -13;
			
				hp --;
				invulnerable--;
			}
		}
		
		public function impactAir() {
			
			if(invulnerable==inv_time){
				
				s_damage.play();
				//y_speed= y_speed *-1;
				
				hp --;
				invulnerable--;
				
			}
		}
		
	}
}
	
		